import {Component, OnInit} from '@angular/core';
import {PpypService} from '../../ppyp.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {UsedStorePService} from '../../../used-store/used-store-p.service';

interface TABLE {
  query: any;
  listData: any;
}

interface ADDCOMMONCOMMODITYMODAL {
  visible: boolean;
  query: any;
  listData: any;
  typeOptions: any;
  brandOptions: any;
}

interface ADDUSEDCOMMODITYMODAL {
  visible: boolean;
  query: any;
  listData: any;
  typeOptions: any;
  modelOptions: any;
}

enum USERTYPE {
  '通用',
  'C端',
  'B端',
}

enum STATUS {
  '无效',
  '已上架',
  '待上架',
}

enum USEDSTATUS {
  '上架中',
  '待上架',
  '已售出',
  '售后中',
  '已冻结',
  '草稿',
  '审核不通过',
  '待审核',
}

enum GOODSTYPE {
  '普通商品',
  '二手商品',
}


enum COMMODITYSTATUS {
  '无效',
  '已上架',
  '未上架'
}

@Component({
  selector: 'app-ppyp-commodity-one',
  templateUrl: './ppyp-commodity-one.component.html',
  styleUrls: ['./ppyp-commodity-one.component.scss']
})
export class PpypCommodityOneComponent implements OnInit {
  COMMODITYSTATUS = COMMODITYSTATUS;
  USERTYPE = USERTYPE;
  STATUS = STATUS;
  USEDSTATUS = USEDSTATUS;
  GOODSTYPE = GOODSTYPE;
  table: TABLE = {
    query: {
      page: 1,
      pageSize: 20,
      showTypes: 1
    },
    listData: {
      page: 1,
      total: 100,
      pageSize: 20,
      records: []
    },
  };
  // 添加普通商品
  addCommonCommodityModal: ADDCOMMONCOMMODITYMODAL = {
    visible: false,
    typeOptions: [],
    brandOptions: [],
    listData: {
      page: 1,
      total: 100,
      pageSize: 20,
      records: []
    },
    query: {
      goods: '',
      showTypes: 1,
      typeList: [],
      brandList: [],
      goodsType: '',
      page: 1,
      pageSize: 10,
    }
  };
  // 添加二手商品
  addUsedCommodityModal: ADDUSEDCOMMODITYMODAL = {
    visible: false,
    typeOptions: [],
    modelOptions: [],
    listData: {
      page: 1,
      total: 100,
      pageSize: 20,
      records: []
    },
    query: {
      equipmentCode: '',
      typeIds: [],
      modelIds: [],
      showTypes: 1,
      page: 1,
      pageSize: 10,
    }
  };
  // 推荐
  recommendModal = {
    visible: false,
    id: '',
    name: '',
    img: ''
  };

  constructor(
    public ppypService: PpypService,
    public nzMessageService: NzMessageService,
    private usedStorePService: UsedStorePService,
  ) {
  }

  ngOnInit(): void {
    this.getTableList();
  }

  onPageIndexChange(index: number) {
    this.table.query.page = index;
    this.getTableList();
  }

  onPageSizeChange(pageSize: number) {
    this.table.query.pageSize = pageSize;
    this.getTableList();
  }

  queryBtn() {
    this.table.query.page = 1;
    this.getTableList();
  }

  resetCondition() {
    this.table.query = {
      page: 1,
      pageSize: 20,
      showTypes: 1
    };
    this.queryBtn();
  }

  // 获取展示区域列表
  getTableList() {
    this.table.listData.records = [];
    this.ppypService.commodityArea(this.table.query).then((res: any) => {
      if (res.code === 0) {
        this.table.listData = res.data;
      }
    });
  }

  // 普通商品
  onCommonPageIndexChange(index: number) {
    this.addCommonCommodityModal.query.page = index;
    this.getCommonCommodity();
  }

  onCommonPageSizeChange(pageSize: number) {
    this.addCommonCommodityModal.query.pageSize = pageSize;
    this.getCommonCommodity();
  }

  getCommonCommodity(resetPage = false) {
    if (resetPage) {
      this.addCommonCommodityModal.query.page = 1;
    }
    this.addCommonCommodityModal.listData.records = [];
    this.ppypService.getCategoryCommodity(this.addCommonCommodityModal.query).then((res: any) => {
      if (res.code === 0) {
        this.addCommonCommodityModal.listData = res.data;
      }
    });
  }

  resetCommonCommodityCondition() {
    this.addCommonCommodityModal.query = {
      goods: '',
      typeList: [],
      brandList: [],
      goodsType: '',
      showTypes: 1,
      page: 1,
      pageSize: 10,
    };
    this.getCommonCommodity();
  }

  showAddCommonCommodityModal() {
    this.addCommonCommodityModal.visible = true;
    this.ppypService.categoryCommodityType().then((res: any) => {
      if (res.code === 0) {
        this.addCommonCommodityModal.typeOptions = this.addFieldArr(res.data);
      }
    });
    this.ppypService.categoryCommodityBrandList().then((res: any) => {
      if (res.code === 0) {
        this.addCommonCommodityModal.brandOptions = res.data;
      }
    });
    this.getCommonCommodity();
  }

  hideAddCommonCommodityModal() {
    this.addCommonCommodityModal = {
      visible: false,
      typeOptions: [],
      brandOptions: [],
      listData: {
        page: 1,
        total: 100,
        pageSize: 20,
        records: []
      },
      query: {
        goods: '',
        typeList: [],
        brandList: [],
        goodsType: '',
        showTypes: 1,
        page: 1,
        pageSize: 10,
      }
    };
  }

  // 二手商品
  onUsedPageIndexChange(index: number) {
    this.addUsedCommodityModal.query.page = index;
    this.getCommodityUsedList();
  }

  onUsedPageSizeChange(pageSize: number) {
    this.addUsedCommodityModal.query.pageSize = pageSize;
    this.getCommodityUsedList();
  }

  showUsedCommonCommodityModal() {
    this.ppypService.getUsedBrandList().then((res: any) => {
      if (res.code === 0) {
        this.addUsedCommodityModal.typeOptions = res.data.records;
      }
    });
    this.addUsedCommodityModal.visible = true;
    this.getCommodityUsedList();
  }

  resetUsedCommodityCondition() {
    this.addUsedCommodityModal.modelOptions = [];
    this.addUsedCommodityModal.query = {
      equipmentCode: '',
      typeIds: [],
      modelIds: [],
      showTypes: 1,
      page: 1,
      pageSize: 10,
    };
    this.getCommodityUsedList();
  }

  getCommodityUsedList(resetPage = false) {
    if (resetPage) {
      this.addUsedCommodityModal.query.page = 1;
    }
    this.ppypService.commodityUsedList({
      ...this.addUsedCommodityModal.query,
      typeIds: this.addUsedCommodityModal.query.typeIds.join(','),
      modelIds: this.addUsedCommodityModal.query.modelIds.join(','),
    }).then((res: any) => {
      if (res.code === 0) {
        this.addUsedCommodityModal.listData = res.data;
      }
    });
  }

  getTypeList() {
    this.addUsedCommodityModal.query.modelIds = [];
    if (!this.addUsedCommodityModal.query.typeIds.join(',')) {
      this.addUsedCommodityModal.modelOptions = [];
      return;
    }
    this.ppypService.getUsedModelList(this.addUsedCommodityModal.query.typeIds.join(',')).then((res: any) => {
      if (res.code === 0) {
        this.addUsedCommodityModal.modelOptions = res.data.records;
      }
    });
  }

  hideAddUsedCommodityModal() {
    this.addUsedCommodityModal = {
      visible: false,
      typeOptions: [],
      modelOptions: [],
      listData: {
        page: 1,
        total: 100,
        pageSize: 20,
        records: []
      },
      query: {
        equipmentCode: '',
        typeIds: [],
        modelIds: [],
        showTypes: 1,
        page: 1,
        pageSize: 10,
      }
    };
  }

  hideRecommendModal() {
    this.recommendModal = {
      visible: false,
      id: '',
      name: '',
      img: ''
    };
  }

  saveRecommend() {
    if (!this.recommendModal.img) {
      this.nzMessageService.warning('请选择主图');
      return;
    }
    this.ppypService.commodityUpdate({
      id: this.recommendModal.id,
      recommendStatus: 1,
      img: this.recommendModal.img
    }).then((res: any) => {
      if (res.code === 0) {
        this.getTableList();
        this.hideRecommendModal();
      }
    });
  }

  move(id: any, type: 0 | 1) {
    this.ppypService.commodityMove(id, type, 1).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getTableList();
      }
    });
  }

  changeStatus(data: any, status: number) {
    if (status === 1) {
      this.recommendModal = {
        visible: true,
        id: data.id,
        name: data.goodsName,
        img: data.img
      };
      return;
    }
    this.ppypService.commodityUpdate({id: data.id, recommendStatus: status}).then((res: any) => {
      if (res.code === 0) {
        console.log(res);
        this.getTableList();
      }
    });
  }

  // 0普通商品 1二手商品
  addCommodity(data: any, source: number) {
    /**
     * id  number  商品来源：0-普通商品、1-二手商品
     * source  number  商品来源：0-普通商品、1-二手商品
     * img  string  商品主图
     * showTypes  number  推荐位置，1-位置1、2-位置2
     */
    // 商品展示区域添加
    this.ppypService.commodityAreaAdd({
      id: data.id,
      source,
      img: data.icon,
      showTypes: 1
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.hideAddCommonCommodityModal();
        this.hideAddUsedCommodityModal();
        this.queryBtn();
      }
    });

  }

  deleteCommodity(id: any) {
    this.ppypService.commodityAreaDelete(id, 1).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.queryBtn();
      }
    });
  }

  // select商品分类改变
  // onChangeTree(event: any) {
  //   if (event) {
  //     this.queryForm.goodsTypeId = event.join(',');
  //   }
  // }

  // 商品分类下拉数据处理
  addFieldArr(arr: any, key?: any, value: any = null, level: any = 0) {
    return arr.map((item: any) => {
      if (item.children && item.children.length > 0) {
        item.disabled = true;
        item.children = this.addFieldArr(item.children);
      }

      if (item.children && item.children.length <= 0) {
        item.isLeaf = true;
      }

      return {
        ...item,
        key: item.id,
        title: item.name
      };
    });
  }
}
